package com.nainiujia.jinlong.aahelper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private LinearLayout content_layout;

    private int item_length = 0;

    private List<View> mViews;

    private EditText et_payment;

    private Button but_calc;

    private Button but_exit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        item_length = getIntent().getIntExtra(InitActivity.People_Key, 0);

        mViews = new ArrayList<View>();

        content_layout = (LinearLayout) findViewById(R.id.content_layout);
        et_payment = (EditText) findViewById(R.id.pay_amount_et);

        but_calc = (Button) findViewById(R.id.pay_calc_but);

        but_exit = (Button) findViewById(R.id.pay_exit_but);

        but_calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                clacPayment();

            }
        });

        but_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doExxit();
            }
        });

        initview();
    }

    private void doExxit() {

        finish();
    }

    private void initview() {

        for (int i = 0; i < item_length; i++) {

            View item_lay = LayoutInflater.from(this).inflate(R.layout.item_layout, null);

            TextView tv_no = (TextView) item_lay.findViewById(R.id.item_no_tv);
            tv_no.setText((i + 1) + "");

            TextView tv_pay = (TextView) item_lay.findViewById(R.id.item_pay_tv);
            tv_pay.setText(String.format("%.2f", 0.00));

            mViews.add(item_lay);

            content_layout.addView(item_lay);
        }

    }


    private float getAllCostAmount() {

        float sum = 0;
        for (View item_layout : mViews) {
            EditText editText = (EditText) item_layout.findViewById(R.id.item_cost_et);
            String item_cost_str = editText.getText().toString().trim();

            if (!TextUtils.isEmpty(item_cost_str)) {
                float item_cost = Float.parseFloat(item_cost_str);
                sum += item_cost;
            }
        }

        return sum;

    }

    private void clacPayment() {

        float costAllSum = getAllCostAmount();


        if (costAllSum > 0) {


            String allPaynet_str = et_payment.getText().toString();

            if (!TextUtils.isEmpty(allPaynet_str)) {

                float payAllSum = Float.parseFloat(allPaynet_str);

                for (View item_layout : mViews) {


                    EditText editText = (EditText) item_layout.findViewById(R.id.item_cost_et);
                    float item_cost = Float.parseFloat(editText.getText().toString().trim());

                    TextView tv_item_pay = (TextView) item_layout.findViewById(R.id.item_pay_tv);

                    float item_payment = item_cost * payAllSum / costAllSum;


                    tv_item_pay.setText(String.format("%.2f", item_payment));

                }
            }

        }
    }

}
